package me.stone.stanimeclient.page.setting

import android.app.Dialog
import android.os.Bundle
import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import me.stone.stanimeclient.core.model.DarkThemeMode
import me.stone.stanimeclient.core.ui.R

@AndroidEntryPoint
class DarkThemeModeSelectDialog : AppCompatDialogFragment() {

    private val viewModel by viewModels<SettingViewModel>()

    private lateinit var listAdapter: ArrayAdapter<ModeHolder>

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        listAdapter = ArrayAdapter(
            requireContext(),
            android.R.layout.simple_list_item_single_choice
        )

        return MaterialAlertDialogBuilder(requireContext())
            .setTitle(R.string.dark_theme_mode)
            .setPositiveButton(R.string.cancel) { dialog, _ ->
                dialog.dismiss()
            }
            .setSingleChoiceItems(listAdapter, 0) { dialog, position ->
                listAdapter.getItem(position)?.mode?.let {
                    viewModel.updateDarkThemeMode(it)
                }
                dialog.dismiss()
            }
            .create()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                listAdapter.clear()
                listAdapter.addAll(
                    DarkThemeMode.entries.map { ModeHolder(it, it.title) }
                )
                updateSelectedItem(viewModel.currentDarkThemeMode)
            }
        }
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.darkThemeModeFlow.collectLatest {
                    updateSelectedItem(it)
                }
            }
        }
    }

    private fun updateSelectedItem(selected: DarkThemeMode) {
        val selectedPosition = (0 until listAdapter.count).indexOfFirst { index ->
            listAdapter.getItem(index)?.mode == selected
        }
        (dialog as AlertDialog).listView.setItemChecked(selectedPosition, true)
    }

    private val DarkThemeMode.title: String
        get() = when(this) {
            DarkThemeMode.Dark -> getString(R.string.open)
            DarkThemeMode.Light -> getString(R.string.close)
            DarkThemeMode.System -> getString(R.string.flow_system)
        }

    companion object {
        fun newInstance() = DarkThemeModeSelectDialog()
    }

    private data class ModeHolder(val mode: DarkThemeMode, val title: String) {
        override fun toString(): String = title
    }

}